﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LootWindow : MonoBehaviour
{
    private static LootWindow instance;
    public static LootWindow MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<LootWindow>();
            }
            return instance;
        }
    }
    [SerializeField]
    private GameObject previousPage, nextPage; 
    [SerializeField]
    private Text pageNumberText;
    private int pagenumber=0;
    [SerializeField]
    private LootButton[] lootButtons;
    [SerializeField]
    private Item[] items;
    private List<List<Drop>> pages=new List<List<Drop>>();
    [SerializeField]
    private CanvasGroup canvasGroup;
    private List<Drop> tmpItems=new List<Drop>();
    public IInteractable MyInteractable { get; set; }
    private bool IsOpened
    {
        get
        {
            return canvasGroup.alpha > 0;
        }
    }
    // Start is called before the first frame update
    void Awake()
    {
        // List<Item> tmp = new List<Item>();
        // for(int i = 0; i < items.Length; i++)
        //{
        //    tmp.Add(items[i]);
        //}
        //CreatePages(tmp);
        //AddLoot();
        canvasGroup = GetComponent<CanvasGroup>();
    }
    public void CreatePages(List<Drop> items)
    {
        if (!IsOpened)
        {

            List<Drop> page = new List<Drop>();
            tmpItems = items;
            for (int i = 0; i < items.Count; i++)
            {
                page.Add(items[i]);
                if (page.Count == 4 || i == items.Count - 1)
                {
                    pages.Add(page);
                    page = new List<Drop>();


                }

            }
            AddLoot();
            Open();
        }

    }
   private void AddLoot()
    {

        if (pages.Count > 0)
        {
            pageNumberText.text =pagenumber + 1 + "/"+ pages.Count;
            previousPage.SetActive(pagenumber > 0);
            nextPage.SetActive(pages.Count > 1 && pagenumber<pages.Count-1);
            
            for (int i = 0; i < pages[pagenumber].Count; i++)
            {
                if (pages[pagenumber][i] != null)
                {
                    lootButtons[i].MyIcon.sprite = pages[pagenumber][i].MyItem.MyIcon;
                    lootButtons[i].gameObject.SetActive(true);
                    lootButtons[i].MyLoot = pages[pagenumber][i].MyItem;
                    string title = string.Format("<color={0}>{1}</color>", 
                        ItemRarityColor.MyRarityColours[pages[pagenumber][i].MyItem.MyItemRarity], 
                        pages[pagenumber][i].MyItem.MyTitle);
                    lootButtons[i].MyText.text = title;
                }
                
            }
        }
        
    }
    public void NextPage()
    {
        if (pagenumber < (pages.Count-1))
        {
            
            ClearButtons();
            pagenumber++;
            AddLoot();
        }
    }
    public void PreviousPage()
    {
        if (pages.Count>0)
        {
            
            ClearButtons();
            pagenumber--;
            AddLoot(); 
        }
    }
    private void ClearButtons()
    {
        foreach (LootButton temp in lootButtons)
        {
            temp.gameObject.SetActive(false);
        }
    }
    public void TakeLoot(Item temp)
    {
        Drop drop = pages[pagenumber].Find(x => x.MyItem == temp);
        pages[pagenumber].Remove(drop);
        drop.Remove();
        if (pages[pagenumber].Count == 0)
        {
            pages.Remove(pages[pagenumber]);

            if (pagenumber > 0 && pagenumber == pages.Count)
            {
                pagenumber--;
            }
            AddLoot();
        }
        
    }
    public void Open()
    {
        canvasGroup.alpha = 1;
        canvasGroup.blocksRaycasts = true;
    }
    public void Close()
    {
        canvasGroup.alpha = 0;
        canvasGroup.blocksRaycasts = false;
        //Player.MyInstance.MyInteractable = null;
        pages.Clear();
        ClearButtons();
        if (MyInteractable != null)
        {
            MyInteractable.StopInteract();
        }
        MyInteractable = null;
    }
}
